package net.lc7.proto;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: Jason.zhu
 * @Create: 2019/05/16 16:19
 */

public class ProtoClientACL {
    static Logger logger = LoggerFactory.getLogger(ProtoClientACL.class);

    public static void main(String[] args) throws Exception{
        ZooKeeper zooKeeper = ZKConnect.getZooKeeper();

        String scheme = "digest";
        String  auth = DigestAuthenticationProvider.generateDigest("jason:jason123");
        System.out.println("auth" + auth);

        List<ACL> aclList = new ArrayList<>();
        Id user1 = new Id(scheme, auth);
        aclList.add(new ACL(ZooDefs.Perms.ALL, user1));
        String result = zooKeeper.create("/aclNode","acl data".getBytes(), aclList, CreateMode.PERSISTENT);
        if(null != result){
            System.out.println("节点:"+result+"创建成功");
        }

        /*List<ACL> acls = zooKeeper.getACL("/aclNode",null);
        for(ACL acl : acls){
            System.out.println();
            System.out.println("权限scheme id：" + acl.getId());
            System.out.println("权限位：" + acl.getPerms());
        }*/

        zooKeeper.addAuthInfo(scheme,"jason:jason123".getBytes());
        System.out.println(new String(zooKeeper.getData("/aclNode",true, null)));

        zooKeeper.delete("/aclNode", -1);


        zooKeeper.close();

    }
}
